namespace NFreeMarker.Core
{
    /// <summary>
    /// An instruction that outputs the value of an <see cref="Expression"/>.
    /// </summary>
    public sealed class DollarVariable : TemplateElement // NOTE: Was internal
    {
        private readonly Expression _expression;
        private readonly Expression _escapedExpression;

        internal DollarVariable(Expression expression, Expression escapedExpression)
        {
            _expression = expression;
            _escapedExpression = escapedExpression;
        }

        /**
         * Outputs the string value of the enclosed expression.
         */
        internal override void Accept(Environment env)
        {
            env.GetOut().Write(_escapedExpression.GetStringValue(env));
        }

        public override string GetCanonicalForm()
        {
            return "${" + _expression.GetCanonicalForm() + "}";
        }

        public override string GetDescription()
        {
            return GetSource() +
                   (_expression == _escapedExpression
                        ? ""
                        : " escaped ${" + _escapedExpression.GetCanonicalForm() + "}");
        }

        internal override bool HeedsOpeningWhitespace()
        {
            return true;
        }

        internal override bool HeedsTrailingWhitespace()
        {
            return true;
        }
    }
}